package com.yl.db_core.utils.properties;

import org.apache.logging.log4j.util.PropertiesUtil;

import java.io.*;
import java.util.*;

/**
 * @Description: Properties 工具栏
 * @Author: YL
 * @CreateDate: 2018/11/13 14:04
 * @Version: 1.0
 */
public class AppPropertiesUtil {

    private String filePath;

    public AppPropertiesUtil(String filePath) {
        this.filePath = filePath;
    }

    /**
     * 获取全部Properties信息
     *
     * @return
     */
    public Map findAllPrpperties() {
        Properties properties = new Properties();
        Map map = new LinkedHashMap();
        try {
            //InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath));
            InputStream inputStream = new PropertiesUtil(properties).getClass().getResourceAsStream(filePath);
            properties.load(inputStream);
            Enumeration enumeration = properties.propertyNames();
            while (enumeration.hasMoreElements()) {
                String key = String.valueOf(enumeration.nextElement());
                String value = String.valueOf(properties.getProperty(key));
                map.put(key, value);
            }
            System.out.println("map:" + map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 通过key获取Properties信息
     *
     * @param key
     * @return
     */
    public String findValueByKey(String key) {
        Properties properties = new Properties();
        try {
            //InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath));
            InputStream inputStream = new PropertiesUtil(properties).getClass().getResourceAsStream(filePath);
            properties.load(inputStream);
            String value = String.valueOf(properties.getProperty(key));
            System.out.println("value:" + value);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过key设置Properties信息
     * 暂时不可用 拒绝访问
     *
     * @param key
     * @param value
     */
    public void setValueByKey(String key, String value) {
        Properties properties = new Properties();
        try {
            //InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath));
            //InputStream inputStream = new PropertiesUtil(properties).getClass().getResourceAsStream(filePath);
            InputStream inputStream = AppPropertiesUtil.class.getClassLoader().getResourceAsStream(filePath);
            properties.load(inputStream);
            OutputStream outputStream = new FileOutputStream(filePath);
            properties.setProperty(key, value);
            properties.store(outputStream, "Update " + key + " name");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        AppPropertiesUtil propertiesUtil = new AppPropertiesUtil("/application.properties");
        //propertiesUtil.findAllPrpperties("/application.properties");
        propertiesUtil.setValueByKey("com.yl.demo.name", "YYYSSS");
        //propertiesUtil.findValueByKey("/application.properties", "com.yl.demo.name");
    }
}
